package com.bytz.modules.cms.domain.impl.order;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.domain.MallOrderProcessor;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.order.model.OrderModel;
import com.bytz.modules.cms.productLine.entity.ProductLine;
import com.bytz.modules.cms.user.entity.CmsSysUser;
import org.apache.commons.lang3.StringUtils;

/**
 * 订单销售处理器 7
 * @author shanxuezhong
 */
public class MallOrderSalesProcessor extends MallOrderAbstractProcessor implements MallOrderProcessor {

    public MallOrderSalesProcessor(int sequence) {
        super(sequence);
    }

    @Override
    public void doProcess(MallOrderProcessContext context) {
        boolean submit = BooleanUtil.isTrue(context.getSubmit());

        OrderModel order = context.getOrder();

        if (StringUtils.isBlank(order.getSalesId())) {
            if (submit) {
                throw new BytzBootException("销售人员不能为空");
            }
        } else {
            // 检查是否是经销商绑定的销售并填入销售名
            CmsSysUser seller = context.getSeller();
            if (seller == null) {
                throw new BytzBootException("找不到订单中指定的销售人员");

            }
            order.setSalesName(seller.getRealname());
            if (submit) {
                ProductLine sellerProductLine = context.getSellerProductLine();
                if (sellerProductLine == null || StrUtil.isBlank(sellerProductLine.getProductLineCode())) {
                    throw new BytzBootException("暂无法提交订单，请先联系销售人员分派产品线");
                }
                order.setProductLineCode(sellerProductLine.getProductLineCode());
            }

        }
    }
}